// var dzBLE from  = require('./DzBLE.js');
import dzBLE from './DzBLE.js'
import bitmapPackage from './BitmapPackage.js'
// import BarcodeWriter from './BarcodeWriter.js'
// var bitmapPackage = require('./BitmapPackage.js');
// var barcodeWriter = require('./BarcodeWriter.js');
const  {
	currentPrinterInfo
} = dzBLE
var _0x5648 = ['translate', 'DPI', 'save', '没有发现打印机', 'measureText', 'toUpperCase', 'createOffscreenCanvas', 'qrcode',
	'setSupportPrefixs', 'height', 'connectPeripheral', 'disconnectPeripheral', 'services', 'data', '#FFFFFF',
	'indexOf', 'getImageData', 'onload', 'getBLEDeviceServices', 'refreshScanPeripherals', 'deviceName', 'src',
	'length', 'setCurrentPrinterInfoProperty', 'RSSI', 'px\x20sans-serif', 'restore', 'currentConnectedDeviceId',
	'fillText', 'rotate', 'sendData', 'drawImage', 'deviceId', 'width', 'name'
];
var _0x50f9 = function(_0x5648d4, _0x50f95c) {
	_0x5648d4 = _0x5648d4 - 0x0;
	var _0x29bfae = _0x5648[_0x5648d4];
	return _0x29bfae;
};
var printWidth = 0x0;
var printHeight = 0x0;
var printOrientation = 0x0;
var itemOrientation = 0x0;
var itemHorizontalAlignment = 0x0;
var itemVerticalAlignment = 0x0;
var printPageGapType = -0x1;
var printPageGapLength = 0x2;
var printDarkness = 0x0;
var printSpeed = 0x0;
var canvas = null;
var context = null;
var isConnecting = ![];

function covert(_0x3c9251) {
	var _0x8072ed = _0x3c9251 * DPI() / 25.4;
	_0x8072ed = _0x8072ed >= 0x0 ? _0x8072ed + 0.1 : _0x8072ed - 0.1;
	return parseInt(_0x8072ed);
}

function DPI() {
	var _0x1811b9 = currentPrinterInfo()[_0x50f9('0x1')];
	return _0x1811b9;
}

function setPrintPageGapType(_0x3a0470) {
	printPageGapType = _0x3a0470;
}

function setPrintPageGapLength(_0x3a1e84) {
	printPageGapLength = _0x3a1e84;
}

function setPrintDarkness(_0x42880c) {
	printDarkness = _0x42880c;
}

function setPrintSpeed(_0x2404f0) {
	printSpeed = _0x2404f0;
}

function setSupportPrefixs(_0x105ae5) {
	dzBLE[_0x50f9('0x8')](_0x105ae5);
}

function connectingPrinterName() {
	return currentPrinterInfo()['name'];
}

function scanedPrinters(_0x1f385c) {
	dzBLE['startScanPeripherals'](function(_0x46fa10) {
		if (_0x1f385c) {
			_0x1f385c(_0x46fa10);
		}
	});
}

function compare(_0x41f916) {
	return function(_0x1c784d, _0x5011d4) {
		var _0x5555eb = _0x1c784d[_0x41f916];
		var _0x5151c4 = _0x5011d4[_0x41f916];
		return _0x5151c4 - _0x5555eb;
	};
}

function openPrinter(_0x1495d5, _0x3236b4, _0x1ba0df) {
	wx[_0x50f9('0x12')]({
		'deviceId': dzBLE['currentConnectedDeviceId'](),
		'success': function(_0x48ec74) {
			isConnecting = !![];
			if (_0x48ec74) {
				if (_0x48ec74['deviceId'] == null && (_0x48ec74['services'] == null || _0x48ec74[_0x50f9(
						'0xc')][_0x50f9('0x16')] == 0x0)) {
					isConnecting = ![];
				}
			}
		},
		'fail': function(_0x2f69fa) {
			isConnecting = ![];
		},
		'complete': function(_0x44bcd6) {
			if (isConnecting) {
				if (_0x3236b4) {
					_0x3236b4(dzBLE[_0x50f9('0x1b')]());
				}
				return;
			}
			scanedPrinters(function(_0x2f4a60) {
				var _0x332f6a = selectedDevice(_0x2f4a60, _0x1495d5);
				if (_0x332f6a[_0x50f9('0x20')] == null || _0x332f6a[_0x50f9('0x20')]['length'] ==
					0x0) {
					setTimeout(function() {
						dzBLE[_0x50f9('0x13')](function(_0x2b488a) {
							var _0x14cc8e = selectedDevice(_0x2b488a, _0x1495d5);
							if (_0x14cc8e['deviceId'] == null || _0x14cc8e[_0x50f9(
									'0x20')][_0x50f9('0x16')] == 0x0) {
								setTimeout(function() {
									dzBLE['refreshScanPeripherals'](
										function(_0xbbd88f) {
											var _0x3dd12f =
												selectedDevice(
													_0xbbd88f, _0x1495d5
													);
											if (_0x3dd12f['deviceId'] ==
												null || _0x3dd12f[
													'deviceId'][_0x50f9(
													'0x16')] == 0x0) {
												console['log'](_0x50f9(
													'0x3'));
												dzBLE[
														'setCurrentPrinterInfoProperty']
													('name', '');
												if (_0x1ba0df) {
													_0x1ba0df(0x1);
												}
											} else {
												connectDevice(_0x3dd12f,
													_0x3236b4,
													_0x1ba0df);
											}
										});
								}, 0xfa0);
							} else {
								connectDevice(_0x14cc8e, _0x3236b4, _0x1ba0df);
							}
						});
					}, 0xfa0);
				} else {
					connectDevice(_0x332f6a, _0x3236b4, _0x1ba0df);
				}
			});
		}
	});
}

function selectedDevice(_0x147736, _0x16b66b) {
	var _0x58e53d = {
		'deviceId': null,
		'deviceName': null
	};
	if (_0x147736['length'] > 0x0) {
		if (_0x16b66b && _0x16b66b['length'] > 0x0) {
			for (var _0x4b34c5 = 0x0; _0x4b34c5 < _0x147736[_0x50f9('0x16')]; _0x4b34c5++) {
				var _0x4e5561 = _0x147736[_0x4b34c5];
				var _0x93a624 = _0x4e5561[_0x50f9('0x22')]['substring'](0x0, _0x16b66b['length']);
				if (_0x93a624[_0x50f9('0x5')]()[_0x50f9('0xf')](_0x16b66b[_0x50f9('0x5')]()) >= 0x0) {
					_0x58e53d['deviceId'] = _0x4e5561['deviceId'];
					_0x58e53d[_0x50f9('0x14')] = _0x4e5561['name'];
					break;
				}
			}
		}
		if (_0x16b66b == null || _0x16b66b[_0x50f9('0x16')] == 0x0) {
			_0x147736['sort'](compare(_0x50f9('0x18')));
			_0x58e53d[_0x50f9('0x20')] = _0x147736[0x0]['deviceId'];
			_0x58e53d['deviceName'] = _0x147736[0x0][_0x50f9('0x22')];
		}
	}
	return _0x58e53d;
}

function connectDevice(_0xcde802, _0x47d68d, _0x4ec224) {
	dzBLE[_0x50f9('0x17')](_0x50f9('0x22'), _0xcde802['deviceName']);
	dzBLE[_0x50f9('0xa')](_0xcde802['deviceId'], function() {
		if (_0x47d68d) {
			_0x47d68d(_0xcde802['deviceId']);
		}
	}, function() {
		dzBLE['setCurrentPrinterInfoProperty'](_0x50f9('0x22'), '');
		if (_0x4ec224) {
			_0x4ec224(0x0);
		}
	});
}

function connectingPrinterDetailInfos() {
	return dzBLE['currentPrinterInfo']();
}

function closePrinter() {
	dzBLE[_0x50f9('0xb')]();
}

function startDrawLabel(_0x529e00, _0x2d5bc4, _0x3daeb8) {
	printOrientation = _0x3daeb8;
	printWidth = covert(_0x529e00);
	printHeight = covert(_0x2d5bc4);
	itemOrientation = 0x0;
	itemHorizontalAlignment = 0x0;
	itemVerticalAlignment = 0x0;
	canvas = wx[_0x50f9('0x6')]({
		'type': '2d',
		'width': printWidth,
		'height': printHeight
	});
	if (canvas == null) {
		return;
	}
	context = canvas['getContext']('2d');
	if (context == null) {
		return;
	}
	context['clearRect'](0x0, 0x0, canvas[_0x50f9('0x21')], canvas[_0x50f9('0x9')]);
	context['fillStyle'] = _0x50f9('0xe');
	context['fillRect'](0x0, 0x0, printWidth + 0x5, printHeight + 0x5);
	context['fillStyle'] = '#000000';
}

function endDrawLabel() {
	return context[_0x50f9('0x10')](0x0, 0x0, printWidth, printHeight);
}

function print(_0x29d2e5) {
	const _0x225be3 = context['getImageData'](0x0, 0x0, printWidth, printHeight)[_0x50f9('0xd')];
	var _0x369057 = [];
	var _0x354b5d = printWidth;
	var _0x3700a4 = printHeight;
	if (printOrientation == 0x0) {
		_0x369057 = _0x225be3;
	} else if (printOrientation == 0x5a) {
		for (var _0x27a5cb = 0x0; _0x27a5cb < _0x354b5d; _0x27a5cb++) {
			for (var _0x526b9f = 0x0; _0x526b9f < _0x3700a4; _0x526b9f++) {
				var _0x2478b8 = (_0x27a5cb * _0x3700a4 + _0x526b9f) * 0x4;
				var _0x1264e3 = ((_0x3700a4 - 0x1 - _0x526b9f) * _0x354b5d + _0x27a5cb) * 0x4;
				_0x369057[_0x2478b8 + 0x0] = _0x225be3[_0x1264e3 + 0x0];
				_0x369057[_0x2478b8 + 0x1] = _0x225be3[_0x1264e3 + 0x1];
				_0x369057[_0x2478b8 + 0x2] = _0x225be3[_0x1264e3 + 0x2];
				_0x369057[_0x2478b8 + 0x3] = _0x225be3[_0x1264e3 + 0x3];
			}
		}
		var _0x3b7bd4 = _0x354b5d;
		_0x354b5d = _0x3700a4;
		_0x3700a4 = _0x3b7bd4;
	} else if (printOrientation == 0xb4) {
		for (var _0x526b9f = 0x0; _0x526b9f < _0x3700a4; _0x526b9f++) {
			for (var _0x27a5cb = 0x0; _0x27a5cb < _0x354b5d; _0x27a5cb++) {
				var _0x2478b8 = (_0x526b9f * _0x354b5d + _0x27a5cb) * 0x4;
				var _0x1264e3 = ((_0x3700a4 - 0x1 - _0x526b9f) * _0x354b5d + _0x354b5d - 0x1 - _0x27a5cb) * 0x4;
				_0x369057[_0x2478b8 + 0x0] = _0x225be3[_0x1264e3 + 0x0];
				_0x369057[_0x2478b8 + 0x1] = _0x225be3[_0x1264e3 + 0x1];
				_0x369057[_0x2478b8 + 0x2] = _0x225be3[_0x1264e3 + 0x2];
				_0x369057[_0x2478b8 + 0x3] = _0x225be3[_0x1264e3 + 0x3];
			}
		}
	} else if (printOrientation == 0x10e) {
		for (var _0x27a5cb = 0x0; _0x27a5cb < _0x354b5d; _0x27a5cb++) {
			for (var _0x526b9f = 0x0; _0x526b9f < _0x3700a4; _0x526b9f++) {
				var _0x2478b8 = (_0x27a5cb * _0x3700a4 + _0x526b9f) * 0x4;
				var _0x1264e3 = (_0x526b9f * _0x354b5d + _0x354b5d - 0x1 - _0x27a5cb) * 0x4;
				_0x369057[_0x2478b8 + 0x0] = _0x225be3[_0x1264e3 + 0x0];
				_0x369057[_0x2478b8 + 0x1] = _0x225be3[_0x1264e3 + 0x1];
				_0x369057[_0x2478b8 + 0x2] = _0x225be3[_0x1264e3 + 0x2];
				_0x369057[_0x2478b8 + 0x3] = _0x225be3[_0x1264e3 + 0x3];
			}
		}
		var _0x3b7bd4 = _0x354b5d;
		_0x354b5d = _0x3700a4;
		_0x3700a4 = _0x3b7bd4;
	}
	var _0x2ef25d = bitmapPackage['arrayWithImage'](_0x369057, currentPrinterInfo()[_0x50f9('0x1')],
		currentPrinterInfo()[_0x50f9('0x21')], _0x354b5d, _0x3700a4, printPageGapType, printPageGapLength * 0x64,
		printDarkness, printSpeed, 0x80, !![], currentPrinterInfo()['softFlags']);
	dzBLE[_0x50f9('0x1e')](_0x2ef25d, function() {
		if (_0x29d2e5) {
			_0x29d2e5();
		}
	});
}

function setItemOrientation(_0x8a0000) {
	if (_0x8a0000 != 0x0 && _0x8a0000 != 0x5a && _0x8a0000 != 0xb4 && _0x8a0000 != 0x10e) {
		return;
	}
	itemOrientation = _0x8a0000;
}

function setItemHorizontalAlignment(_0x3d970a) {
	if (_0x3d970a != 0x0 && _0x3d970a != 0x1 && _0x3d970a != 0x2) {
		return;
	}
	itemHorizontalAlignment = _0x3d970a;
}

function setItemVerticalAlignment(_0x1cc7f2) {
	if (_0x1cc7f2 != 0x0 && _0x1cc7f2 != 0x1 && _0x1cc7f2 != 0x2) {
		return;
	}
	itemVerticalAlignment = _0x1cc7f2;
}

function drawTextInWidth(_0x585ad6, _0x170426, _0x728374, _0xd29875, _0x33723a) {
	if (_0x585ad6 == null || _0x585ad6['length'] <= 0x0) {
		return ![];
	}
	var _0xe015a5 = covert(_0x33723a) + _0x50f9('0x19');
	var _0x5d5789 = covert(_0x170426);
	var _0xe4893b = covert(_0x728374);
	var _0x1a8cc9 = covert(_0xd29875);
	context['font'] = _0xe015a5;
	const _0x4415a7 = context[_0x50f9('0x4')](_0x585ad6)[_0x50f9('0x21')];
	switch (itemHorizontalAlignment) {
		case 0x0: {
			break;
		}
		case 0x1: {
			_0x5d5789 += (_0x1a8cc9 - _0x4415a7) * 0.5;
			break;
		}
		case 0x2: {
			_0x5d5789 += _0x1a8cc9 - _0x4415a7;
			break;
		}
		default:
			break;
	}
	context[_0x50f9('0x1c')](_0x585ad6, _0x5d5789, _0xe4893b);
	return !![];
}

function drawText(_0x56448c, _0x15b4b0, _0x4f80a1, _0x22c4dc) {
	setItemHorizontalAlignment(0x0);
	drawTextInWidth(_0x56448c, _0x15b4b0, _0x4f80a1, printWidth, _0x22c4dc);
}

function drawBarcode(_0x5206b2, _0x1a56e9, _0x5b1abc, _0x489195, _0x54cbfb) {
	_0x1a56e9 = covert(_0x1a56e9);
	_0x5b1abc = covert(_0x5b1abc);
	_0x489195 = covert(_0x489195);
	_0x54cbfb = covert(_0x54cbfb);
	context[_0x50f9('0x2')]();
	switch (itemOrientation) {
		case 0x1:
		case 0x5a: {
			context['rotate'](Math['PI'] / 0x2);
			context[_0x50f9('0x0')](parseInt(_0x5b1abc - _0x1a56e9), parseInt(-_0x1a56e9 - _0x5b1abc - _0x489195));
			var _0x5a6705 = _0x489195;
			_0x489195 = _0x54cbfb;
			_0x54cbfb = _0x5a6705;
			break;
		}
		case 0x2:
		case 0xb4: {
			context[_0x50f9('0x1d')](Math['PI']);
			context['translate'](parseInt(-_0x1a56e9 * 0x2 - _0x489195), parseInt(-_0x5b1abc * 0x2 - _0x54cbfb));
			break;
		}
		case 0x3:
		case 0x10e: {
			context['rotate'](-Math['PI'] / 0x2);
			context['translate'](parseInt(-_0x1a56e9 - _0x5b1abc - _0x54cbfb), parseInt(_0x1a56e9 - _0x5b1abc));
			var _0x5a6705 = _0x489195;
			_0x489195 = _0x54cbfb;
			_0x54cbfb = _0x5a6705;
			break;
		}
		default:
			break;
	}
	// barcodeWriter['barcode'](context, _0x5206b2, _0x1a56e9, _0x5b1abc, _0x489195, _0x54cbfb);
	context['restore']();
}

function drawQRCode(_0x5d083d, _0x2db9d3, _0x8ce275, _0x1d17f5, _0x39caf9, _0x39b5e3) {
	_0x2db9d3 = covert(_0x2db9d3);
	_0x8ce275 = covert(_0x8ce275);
	_0x1d17f5 = covert(_0x1d17f5);
	_0x39caf9 = _0x39caf9 == null ? _0x1d17f5 : _0x39caf9;
	_0x39caf9 = covert(_0x39caf9);
	context['save']();
	switch (itemOrientation) {
		case 0x1:
		case 0x5a: {
			context[_0x50f9('0x1d')](Math['PI'] / 0x2);
			context[_0x50f9('0x0')](parseInt(_0x8ce275 - _0x2db9d3), parseInt(-_0x2db9d3 - _0x8ce275 - _0x1d17f5));
			var _0x23ad5b = _0x1d17f5;
			_0x1d17f5 = _0x39caf9;
			_0x39caf9 = _0x23ad5b;
			break;
		}
		case 0x2:
		case 0xb4: {
			context[_0x50f9('0x1d')](Math['PI']);
			context[_0x50f9('0x0')](parseInt(-_0x2db9d3 * 0x2 - _0x1d17f5), parseInt(-_0x8ce275 * 0x2 - _0x39caf9));
			break;
		}
		case 0x3:
		case 0x10e: {
			context['rotate'](-Math['PI'] / 0x2);
			context['translate'](parseInt(-_0x2db9d3 - _0x8ce275 - _0x39caf9), parseInt(_0x2db9d3 - _0x8ce275));
			var _0x23ad5b = _0x1d17f5;
			_0x1d17f5 = _0x39caf9;
			_0x39caf9 = _0x23ad5b;
			break;
		}
		default:
			break;
	}
	barcodeWriter[_0x50f9('0x7')](context, _0x5d083d, _0x2db9d3, _0x8ce275, _0x1d17f5, _0x39caf9, _0x39b5e3);
	context[_0x50f9('0x1a')]();
}

function drawLine(_0x2bfa25, _0x14dbb3, _0x468312, _0x188f6b) {
	drawRectangle(_0x2bfa25, _0x14dbb3, _0x468312, _0x188f6b, 0x0, !![]);
}

function drawRectangle(_0x63f7d8, _0x365b5b, _0x49f47f, _0x10ee6e, _0x2aa2e1, _0x2b423d) {
	_0x63f7d8 = covert(_0x63f7d8);
	_0x365b5b = covert(_0x365b5b);
	_0x49f47f = covert(_0x49f47f);
	_0x10ee6e = covert(_0x10ee6e);
	_0x2aa2e1 = covert(_0x2aa2e1);
	context['save']();
	switch (itemOrientation) {
		case 0x1:
		case 0x5a: {
			context[_0x50f9('0x1d')](Math['PI'] / 0x2);
			context['translate'](parseInt(_0x365b5b - _0x63f7d8), parseInt(-_0x63f7d8 - _0x365b5b - _0x49f47f));
			var _0x42e69a = _0x49f47f;
			_0x49f47f = _0x10ee6e;
			_0x10ee6e = _0x42e69a;
			break;
		}
		case 0x2:
		case 0xb4: {
			context[_0x50f9('0x1d')](Math['PI']);
			context[_0x50f9('0x0')](parseInt(-_0x63f7d8 * 0x2 - _0x49f47f), parseInt(-_0x365b5b * 0x2 - _0x10ee6e));
			break;
		}
		case 0x3:
		case 0x10e: {
			context['rotate'](-Math['PI'] / 0x2);
			context['translate'](parseInt(-_0x63f7d8 - _0x365b5b - _0x10ee6e), parseInt(_0x63f7d8 - _0x365b5b));
			var _0x42e69a = _0x49f47f;
			_0x49f47f = _0x10ee6e;
			_0x10ee6e = _0x42e69a;
			break;
		}
		default:
			break;
	}
	context['lineWidth'] = _0x2aa2e1;
	if (_0x2b423d) {
		context['fillStyle'] = '#000000';
		context['fillRect'](_0x63f7d8, _0x365b5b, _0x49f47f, _0x10ee6e);
	} else {
		context['strokeRect'](_0x63f7d8, _0x365b5b, _0x49f47f, _0x10ee6e);
	}
	context['restore']();
}

function drawImagePath(_0x4f069d, _0x272c91, _0x4fa97b, _0x153f05, _0x644682, _0x15b201) {
	_0x272c91 = covert(_0x272c91);
	_0x4fa97b = covert(_0x4fa97b);
	_0x153f05 = covert(_0x153f05);
	_0x644682 = covert(_0x644682);
	context['save']();
	switch (itemOrientation) {
		case 0x1:
		case 0x5a: {
			context['rotate'](Math['PI'] / 0x2);
			context[_0x50f9('0x0')](parseInt(_0x4fa97b - _0x272c91), parseInt(-_0x272c91 - _0x4fa97b - _0x153f05));
			var _0x481132 = _0x153f05;
			_0x153f05 = _0x644682;
			_0x644682 = _0x481132;
			break;
		}
		case 0x2:
		case 0xb4: {
			context[_0x50f9('0x1d')](Math['PI']);
			context[_0x50f9('0x0')](parseInt(-_0x272c91 * 0x2 - _0x153f05), parseInt(-_0x4fa97b * 0x2 - _0x644682));
			break;
		}
		case 0x3:
		case 0x10e: {
			context['rotate'](-Math['PI'] / 0x2);
			context['translate'](parseInt(-_0x272c91 - _0x4fa97b - _0x644682), parseInt(_0x272c91 - _0x4fa97b));
			var _0x481132 = _0x153f05;
			_0x153f05 = _0x644682;
			_0x644682 = _0x481132;
			break;
		}
		default:
			break;
	}
	const _0x191726 = wx[_0x50f9('0x6')]({
		'type': '2d',
		'width': _0x153f05,
		'height': _0x644682
	});
	const _0x324124 = _0x191726['createImage']();
	_0x324124[_0x50f9('0x15')] = _0x4f069d;
	_0x324124[_0x50f9('0x11')] = () => {
		context[_0x50f9('0x1f')](_0x324124, _0x272c91, _0x4fa97b, _0x153f05, _0x644682);
		context[_0x50f9('0x1a')]();
		if (_0x15b201) {
			_0x15b201();
		}
	};
}
export default {
	'setPrintPageGapType': setPrintPageGapType,
	'setPrintPageGapLength': setPrintPageGapLength,
	'setPrintDarkness': setPrintDarkness,
	'setPrintSpeed': setPrintSpeed,
	'setSupportPrefixs': setSupportPrefixs,
	'connectingPrinterName': connectingPrinterName,
	'scanedPrinters': scanedPrinters,
	'openPrinter': openPrinter,
	'connectingPrinterDetailInfos': connectingPrinterDetailInfos,
	'closePrinter': closePrinter,
	'startDrawLabel': startDrawLabel,
	'endDrawLabel': endDrawLabel,
	'print': print,
	'setItemOrientation': setItemOrientation,
	'setItemHorizontalAlignment': setItemHorizontalAlignment,
	'setItemVerticalAlignment': setItemVerticalAlignment,
	'drawText': drawText,
	'drawTextInWidth': drawTextInWidth,
	'drawBarcode': drawBarcode,
	'drawQRCode': drawQRCode,
	'drawLine': drawLine,
	'drawRectangle': drawRectangle,
	'drawImagePath': drawImagePath
};
